﻿using AutoMapper;
using CarAppDomain.CarParts.Entities;
using CarAppDomain.CarParts.IRepository;
using CarAppDomain.CarParts.IService;
using CarAppDomain.CarType.Entities;
using CarAppDomain.CarType.IRepository;
using CarAppDto.CarParts.CarPartsDtos;
using CarAppService.Sys.IServices;
using D.Util.UtilCache;
using D.UtilCore.Dtos;
using D.UtilCore.Utils;
using D.UtilSqlsugarOrm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CarAppDomain.CarParts.Service
{
    public class CarPartsService : BaseService<CarPartsEntity>,ICarPartsService
    {
        private readonly IMapper _mapper;
        private readonly ICacheService _cache;
        private readonly ICarPartsEntityRepository _repository;
        private readonly ICarPartsTypeEntityRepository _repositoryCarPartsType;

        /// <summary>
        /// 构造注入
        /// </summary>
        public CarPartsService(IMapper mapper, ICacheService cache, ICarPartsEntityRepository repository, ICarPartsTypeEntityRepository repositoryCarPartsType)
        {
            // 用于base方法
            _baseDal = repository;
            _repository = repository;
            _repositoryCarPartsType= repositoryCarPartsType;
            _cache = cache;
            _mapper = mapper;
        }

        #region 增删改查


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddCarPartsAsync(ReqAddCarParts reqModel)
        {
            var exists = await _repository.ExistsAsync(m => m.Name == reqModel.Name);
            if (exists)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "名称重复" };
            }
            //exists = await _repositoryCarPartsType.ExistsAsync(m=>m.Id==reqModel.CarPartsTypeId);
            //if (!exists)
            //{
            //    return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "配件类型不存在" };
            //}
            var insertEntity = _mapper.Map<ReqAddCarParts, CarPartsEntity>(reqModel);
            insertEntity.DeleteFlag = false;
            insertEntity.CreateTime = DateTime.Now;
            insertEntity.Id = Guid.NewGuid().ToString();
        
           var dbres = await _repository.AddAsync(insertEntity);
           return dbres.NormalResponse();
         
         
          
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> DeleteCarPartsAsync(List<string> ids)
        {
            var dbres = await _repository.DeleteAsync(m => ids.Contains(m.Id));
            return dbres.NormalResponse();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateCarPartsAsync(ReqUpdateCarParts reqModel)
        {

            var updateEntity = await _repository.GetModelAsync(m => m.Id == reqModel.Id);
            if (updateEntity == null)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "该信息不存在" };
            }
            var exists = await _repository.ExistsAsync(m => m.Id != reqModel.Id && (m.Name == reqModel.Name));
            if (exists)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "名称重复" };
            }
            //exists = await _repositoryCarPartsType.ExistsAsync(m => m.Id == reqModel.CarPartsTypeId);
            //if (!exists)
            //{
            //    return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "配件类型不存在" };
            //}

            updateEntity.Name = reqModel.Name;
            updateEntity.EnglishName = reqModel.EnglishName;
            updateEntity.UpdateTime = DateTime.Now;
            updateEntity.UpdateUser = reqModel.UpdateUser;
            updateEntity.CarPartsTypeId = reqModel.CarPartsTypeId;
            updateEntity.Code= reqModel.Code;
            updateEntity.StartingDate = reqModel.StartingDate;
            updateEntity.EndDate= reqModel.EndDate;
            updateEntity.Image = reqModel.Image;
            updateEntity.CustomFields1 = reqModel.CustomFields1;
            updateEntity.CustomFields2 = reqModel.CustomFields2;
            updateEntity.CustomFields3 = reqModel.CustomFields3;
            updateEntity.CustomFields4 = reqModel.CustomFields4;
            updateEntity.CustomFields5 = reqModel.CustomFields5;
            updateEntity.CustomFields6 = reqModel.CustomFields6;
            updateEntity.CustomFields7 = reqModel.CustomFields7;
            updateEntity.CustomFields8 = reqModel.CustomFields8;
            updateEntity.CustomFields9 = reqModel.CustomFields9;
            updateEntity.CustomFields10 = reqModel.CustomFields10;

            var dbres = await _repository.UpdateAsync(updateEntity);
            return dbres.NormalResponse();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<RepCarParts>>> QueryCarPartsListAsync(ReqQueryCarPartsList reqModel)
        {
            Expression<Func<CarPartsEntity, bool>> where = null;
            if (!string.IsNullOrEmpty(reqModel.Name))
            {
                where = where.And(m => m.Name.Contains(reqModel.Name));
            }

            if (!string.IsNullOrEmpty(reqModel.EnglishName))
            {
                where = where.And(m => m.EnglishName.Contains(reqModel.EnglishName));
            }

            if (!string.IsNullOrEmpty(reqModel.VehicleSeriesId))
            {
                where = where.And(m => m.VehicleSeriesId==reqModel.VehicleSeriesId);
            }

            var dbres = await _repository.GetListAsync(where);
            //var res = _mapper.Map<List<CarPartsEntity>, List<RepCarParts>>(dbres);
            return dbres.GetResponse();
        }

        /// <summary>
        /// 获取用户分页
        /// </summary>
        /// <param name="reqQueryAdminPage"></param>
        /// <returns></returns>
        public async Task<ApiResult<PageModel<RepCarParts>>> QueryCarPartsPagesAsync(ReqQueryCarPartsPage reqQueryPage)
        {
            Expression<Func<CarPartsEntity, bool>> where = null;
            if (!string.IsNullOrEmpty(reqQueryPage.Name))
            {
                where = where.And(m => m.Name.Contains(reqQueryPage.Name));
            }

            if (!string.IsNullOrEmpty(reqQueryPage.EnglishName))
            {
                where = where.And(m => m.EnglishName.Contains(reqQueryPage.EnglishName));
            }
            if (!string.IsNullOrEmpty(reqQueryPage.VehicleSeriesId))
            {
                where = where.And(m => m.VehicleSeriesId == reqQueryPage.VehicleSeriesId);
            }

            var dbres = await _repository.GetPagesAsync(reqQueryPage.PageSize, reqQueryPage.PageNo, where, m => m.CreateTime);
            var res = new PageModel<RepCarParts>()
            {
                PageNo = dbres.PageNo,
                PageSize = dbres.PageSize,
                TotalCount = dbres.TotalCount,
                TotalPage = dbres.TotalPage,
                Data = dbres.Data
            };
            return res.GetResponse();
        }

        /// <summary>
        /// 查询单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApiResult<RepCarParts>> GetCarPartsAsync(string id)
        {
            var dbres = await _repository.GetModelAsync(m => m.Id == id);
            var res = _mapper.Map<CarPartsEntity, RepCarParts>(dbres);
            return res.GetResponse();
        }

        #endregion
    }
}
